﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using COM.Data;
using COM.Web;
using COM.Service;
using SampleApp.Domain;
using SampleApp.Service;
using SampleApp.Web.SysModule.Models;

namespace SampleApp.Web.SysModule.Controllers
{
    public class SysRoleController : Controller
    {
        private ISysRoleService sysRoleService = Unity.Instance.GetService<ISysRoleService>();
        private ISysMenuService sysMenuService = Unity.Instance.GetService<ISysMenuService>();
        private IPermissionService permissionService = Unity.Instance.GetService<IPermissionService>();
        private IRoleActionDataService roleActionDataService = Unity.Instance.GetService<IRoleActionDataService>();
        public ActionResult List(int page = 1)
        {
            //this.permissionService.AllowOperateOrLogin(EnumPermissionNameWeb.SampleAdmin);
            var list = this.sysRoleService.GetSysRoleList();
            return View(list);
        }

        public ActionResult Edit(string id)
        {
            return RedirectToAction(EnumOperation.Update, new { id = id });
        }

        public ViewResult Update(string id)
        {
            var sysRole = this.sysRoleService.GetSysRoleByID(id);
            return View("Edit", sysRole);
        }

        public ActionResult Create()
        {
            var sysRole = new SysRole()
            {
                RoleID = Guid.NewGuid().ToString(),
                RoleName = "新角色",
            };

            return View("Edit",sysRole);
        }


        [HttpPost]
        public ActionResult Create(SysRole sysRole)
        {
            if (ModelState.IsValid)
            {
                this.sysRoleService.CreateSysRole(sysRole);
                return RedirectToAction("List");
            }

            return null;
        }


        [HttpPost]
        public ActionResult Update(SysRole sysRole)
        {
            if (ModelState.IsValid)
            {
                this.sysRoleService.UpdateSysRole(sysRole);
                return RedirectToAction("List");
            }
            return null;
        }

        [HttpPost]
        public ActionResult Delete(string id)
        {
            this.sysRoleService.DeleteSysRole(id);
            return null;
        }

        public ActionResult RoleMenuList(string id)
        {
            var menuAll = this.sysMenuService.GetSysMenuList();
            var selectedMenuList = this.sysRoleService.GetMenuListByRole(id);
            var list = selectedMenuList.Select(i => new RoleMenuModel() 
            { 
                IsSelected = true,
                SortBy = this.GetSortBy(menuAll,i),
                IsParent = menuAll.Exists(p=>p.ParentMenuID == i), 
                MenuID = i, 
                MenuName = menuAll.Where(m => m.MenuID == i).Select(m => m.MenuName).SingleOrDefault() 
            }).ToList();
            //合并未设置的菜单项
            var q = menuAll.Where(i => !selectedMenuList.Any(m => i.MenuID == m))
                            .Select(i => new RoleMenuModel() 
                                    { 
                                        IsSelected = false, 
                                        SortBy = this.GetSortBy(menuAll,i.MenuID),
                                        IsParent = menuAll.Exists(p => p.ParentMenuID == i.MenuID), 
                                        MenuID = i.MenuID, 
                                        MenuName = i.MenuName 
                                    }).ToList();
            var menuListAll = list.Concat(q).ToList().OrderBy(i=>i.SortBy).ToList();
            var model = new RoleMenuListModel() { RoleID = id, MenuList = menuListAll };
            return View(model);
        }

        private string GetSortBy(List<SysMenu> menuAll, string menuID)
        {
            menuID = menuID.TrimOrEmpty();
            var menuItem = menuAll.Where(m => m.MenuID.TrimOrEmpty() == menuID).Single();
            if (menuItem.ParentMenuID.TrimOrEmpty() == "root")
                return menuItem.SortBy.TrimOrEmpty();
            var parentSort = menuAll.Where(m => m.MenuID.TrimOrEmpty() == menuItem.ParentMenuID.TrimOrEmpty()).Select(m => m.SortBy).SingleOrDefault();
            return parentSort.TrimOrEmpty() + menuItem.SortBy.TrimOrEmpty();
        }

        [HttpPost]
        public ActionResult SetRoleMenu(RoleMenuListModel model)
        {
            var menuList = model.MenuList.Where(i => i.IsSelected).Select(i => i.MenuID).ToList();
            this.sysRoleService.SetRoleMenu(model.RoleID, menuList);
            this.roleActionDataService.Reload();
            return null;
        }
    }
}